package com.evil.kingdom.pojo.enums;

import com.evil.common.core.exception.BusinessException;
import com.evil.kingdom.pojo.enums.result.RCodeEnum;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Arrays;
import java.util.Optional;

/**
 * 战斗类型
 *
 * @author liyang
 * @date 2024-07-15 10:45
 */
@Getter
@AllArgsConstructor
public enum BattleTypeEnum {

    AUTO(0, "自动"),
    AWAITING(1, "待机"),
    FORWARD(2, "前进"),
    RETREAT(3, "后退"),
    UPWARD(4, "向上"),
    DOWNWARD(5, "向下"),
    ABOUT_DISPERSE(6, "左右分散"),
    CENTER_CONCENTRATE(7, "中央集中"),
    ;

    private final int type;
    private final String title;

    public static Optional<BattleTypeEnum> isExistByType(Integer type) {
        if (null == type) {
            return Optional.empty();
        }
        return Arrays.stream(values()).filter(e -> e.type == type).findFirst();
    }

    public static BattleTypeEnum findByType(Integer type) {
        return BattleTypeEnum.isExistByType(type).orElseThrow(BattleTypeEnum::error);
    }

    public static BusinessException error() {
        return new BusinessException(RCodeEnum.ERROR_PARAMETER, "战斗类型错误");
    }
}
